/*
 * Copyright 2010 Francisco Modesto
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.gwt.tinyaop.rebind;

import com.google.gwt.core.ext.typeinfo.JArrayType;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.core.ext.typeinfo.JParameter;
import com.google.gwt.core.ext.typeinfo.JType;
import com.google.gwt.user.rebind.SourceWriter;

public class MethodBuilder implements Builder {
  private JMethod method;
  private Builder invocation;
  private boolean isIntercepted;
  
  public MethodBuilder(JMethod method, Builder invocation, boolean isIntercepted) {
    this.method = method;
    this.invocation = invocation;
    this.isIntercepted = isIntercepted;
  }

  @Override
  public void create(SourceWriter writer) {
    writer.println("@Override");
    writer.println(methodDeclaration() + " {");
    writer.indent();
    if (isIntercepted) {
      writer.println("final Object[] _args = new Object[] { " + arguments() + " };");
      writer.println("final Object _this = this;");
    }
    invocation.create(writer);
    writer.outdent();
    writer.println("}");
  }
 
  private String methodDeclaration() {
    StringBuilder sb = new StringBuilder();
    if (!method.isDefaultAccess()) {
      sb.append(method.isPublic() ? "public" : "protected");
    }
    sb.append(" ").append(erasedType(method.getReturnType()));
    sb.append(" ").append(method.getName());
    createParameters(method, sb);
    return sb.toString();
  }

  private void createParameters(JMethod method, StringBuilder sb) {
    JParameter[] params = method.getParameters();
    sb.append("(");
    for (int i = 0, c = params.length; i < c; ++i) {
      JParameter param = params[i];
      if (i != 0) {
        sb.append(", ");
      }
      if (method.isVarArgs() && i == c - 1) {
        JArrayType arrayType = param.getType().isArray();
        assert (arrayType != null);
        sb.append(erasedType(arrayType.getComponentType()));
        sb.append("...");
      }
      else {
        sb.append(erasedType(param.getType()));
      }
      sb.append(" ");
      sb.append("p").append(i);
    }
    sb.append(")");
  }

  private String erasedType(JType type) {
    return type.getErasedType().getQualifiedSourceName();
  }

  private String arguments() {
    JParameter[] params = method.getParameters();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < params.length; i++) {
      if (i != 0) {
        sb.append(", ");
      }
      sb.append("p").append(i);
    }
    return sb.toString();
  }
  
  public static String arguments(JMethod method, boolean isIntercepted) {
    StringBuilder sb = new StringBuilder();
    JParameter[] params = method.getParameters();
    for (int i = 0, c = params.length; i < c; ++i) {
      JParameter param = params[i];
      if (i != 0) {
        sb.append(", ");
      }
      
      if (isIntercepted) {
        sb.append("(").append(castType(param.getType())).append(")");
        sb.append(" ");
        sb.append("_args[").append(i).append("]");
      }
      else {
        sb.append("p").append(i);
      }
    }
    return sb.toString();
  }

  private static String castType(JType type) {
    if (type.isPrimitive() != null) {
      return type.isPrimitive().getQualifiedBoxedSourceName();
    }
    else {
      return type.getErasedType().getQualifiedSourceName();
    }
  }
}
